﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace PT.pages
{
    public partial class addReservation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btInsert_Click(object sender, EventArgs e)
        {
            if (CalendarEnd.SelectedDate.Year == 1 || CalendarStart.SelectedDate.Year == 1)
            {
                btInsert.Text += " - BŁĄD: Zaznacz kalendarze!";
            }
            else
            {
                DateTime startDate = CalendarStart.SelectedDate;
                DateTime endDate = CalendarEnd.SelectedDate;
                if (startDate > endDate)
                {
                    lbError.Text = "Początkowa data jest późniejsza niż końcowa!";
                }
                else
                {
                    if (tbStartHour.Text != "" || tbEndHour.Text != "")
                    {
                        char[] separators = { ':' };
                        string[] startTime = tbStartHour.Text.Split(separators);
                        string[] endTime = tbEndHour.Text.Split(separators);
                        startDate = startDate.AddHours(Convert.ToDouble(startTime[0]));
                        startDate = startDate.AddMinutes(Convert.ToDouble(startTime[1]));

                        endDate = endDate.AddHours(Convert.ToDouble(endTime[0]));
                        endDate = endDate.AddMinutes(Convert.ToDouble(endTime[1]));
                        string conflict = IsConflicted(startDate, endDate);
                        if (conflict == "ok")
                        {
                            SqlDataSource1.InsertParameters["startDate"] = new Parameter("startDate", DbType.DateTime, startDate.ToString());
                            SqlDataSource1.InsertParameters["endDate"] = new Parameter("endDate", DbType.DateTime, endDate.ToString());
                            SqlDataSource1.InsertParameters["bookerName"] = new Parameter("bookerName", DbType.String, tbWho.Text);
                            SqlDataSource1.InsertParameters["telephone"] = new Parameter("telephone", DbType.String, tbTel.Text);
                            if (SqlDataSource1.Insert() > 0)
                            {
                                Response.Redirect("freeRooms.aspx");
                            }
                        }
                        else
                        {
                            lbError.Text = conflict;
                        }
                    }
                    else lbError.Text = "BŁĄD - Wpisz odpowiednie godziny!";
                }
            }
        }

        protected string IsConflicted(DateTime start, DateTime end)
        {
            //string connString = @"Data Source=MDX-PC\SQLEXPRESS;Initial Catalog=Hotel;Integrated Security=True";
            string connString = ConfigurationManager.ConnectionStrings["HotelConnectionString1"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand selectRoomReservations = conn.CreateCommand();
            string roomId = Request.QueryString["id"];
            selectRoomReservations.CommandText = "SELECT * FROM Reservation WHERE room_id =" + roomId;
            conn.Open();
            DataSet resDS = new DataSet();
            SqlDataAdapter resDA = new SqlDataAdapter(selectRoomReservations);
            resDA.Fill(resDS);
            DataTable resTable = resDS.Tables[0];
            string conflict = "ok";
            foreach (DataRow row in resTable.Rows)
            {
                DateTime orgStartDate = (DateTime)row["startDate"];
                DateTime orgEndDate = (DateTime)row["endDate"];
                DateTime today = DateTime.Now;
                if (conflict == "ok")
                {
                    if (start > orgStartDate && start < orgEndDate)
                    {
                        if (end < orgEndDate)
                        {
                            conflict = "Konflikt: " + orgStartDate.ToString() + " <-Początek, Koniec-> " + orgEndDate.ToString();
                        }
                        else if (end > orgEndDate)
                        {
                            conflict = "Konflikt: " + orgStartDate.ToString() + " <-Początek-> " + orgEndDate.ToString() + " <-Koniec";
                        }
                    }
                    else if (start < orgStartDate)
                    {
                        if (end > orgStartDate)
                        {
                            conflict = "Konflikt: " + orgStartDate.ToString() + " <-Koniec->" + orgEndDate.ToString();
                        }
                        else if (end > orgEndDate)
                        {
                            conflict = "Konflikt: Początek->" + orgStartDate.ToString() + " , " + orgEndDate.ToString() + " <-Koniec";
                        }
                    }
                    else if (start < today)
                    {
                        conflict = "Konflikt: Początek starszy niż dzisiejsza data!";
                    }
                }
            }
            return conflict;
        }

        protected void CalendarStart_SelectionChanged(object sender, EventArgs e)
        {
            btInsert.Text = "Dodaj rezerwację";
        }

        protected void CalendarEnd_SelectionChanged(object sender, EventArgs e)
        {
            btInsert.Text = "Dodaj rezerwację";
        }
    }
}
